package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.vo.SysMealOrderVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.system.domain.SysMealOrder;
import com.ruoyi.system.service.ISysMealOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报餐订单Controller
 *
 * @author jijinwu
 * @date 2024-07-19
 */
@Api(tags = "报餐订单")
@RestController
@RequestMapping("/system/order")
public class SysMealOrderController extends BaseController
{
    @Autowired
    private ISysMealOrderService sysMealOrderService;

    /**
     * 查询报餐订单列表
     */
    @ApiOperation("查询报餐订单列表")
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMealOrder sysMealOrder)
    {
        List<SysMealOrder> list1 = new ArrayList<>();
//        startPage();
        List<SysMealOrder> list = sysMealOrderService.selectSysMealOrderList(sysMealOrder);
        logger.info("list:->{}",list);
//        return getDataTable(list);
        return getDataTable(list1);
    }

    /**
     * 导出报餐订单列表
     */
    @ApiOperation(value = "导出报餐订单列表")
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "报餐订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMealOrder sysMealOrder)
    {
        List<SysMealOrder> list = sysMealOrderService.selectSysMealOrderList(sysMealOrder);
        ExcelUtil<SysMealOrder> util = new ExcelUtil<SysMealOrder>(SysMealOrder.class);
        util.exportExcel(response, list, "报餐订单数据");
    }

    /**
     * 获取报餐订单详细信息
     */
    @ApiOperation(value = "获取报餐订单详细信息")
    @ApiImplicitParam(name = "orderId",value = "报餐订单ID")
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping("/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(sysMealOrderService.selectSysMealOrderByOrderId(orderId));
    }

    /**
     * 新增报餐订单
     */
    @ApiOperation(value = "新增报餐订单")
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "报餐订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysMealOrder sysMealOrder)
    {
        return toAjax(sysMealOrderService.insertSysMealOrder(sysMealOrder));
    }

    /**
     * 修改报餐订单
     */
    @ApiOperation(value = "修改报餐订单")
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "报餐订单", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody SysMealOrder sysMealOrder)
    {
        return toAjax(sysMealOrderService.updateSysMealOrder(sysMealOrder));
    }

    /**
     * 删除报餐订单
     */
    @ApiOperation(value = "删除报餐订单")
    @ApiImplicitParam(name = "orderIds", value = "报餐订单ID",paramType = "query", dataType = "Long")
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "报餐订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(sysMealOrderService.deleteSysMealOrderByOrderIds(orderIds));
    }

    @ApiOperation(value = "保存报餐订单")
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "报餐订单", businessType = BusinessType.INSERT)
    @PostMapping("/saveMealOrderLog")
    public AjaxResult saveMealOrderLog(Long[] userIds,Long canteenId,String mealDate){
        LoginUser loginUser = getLoginUser();
        return toAjax(sysMealOrderService.saveMealOrderLog(loginUser,userIds,canteenId,mealDate));
    }

    /**
     * 查询报餐订单列表
     */
    @ApiOperation("查询报餐订单列表")
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/listOrderByUserId")
    public TableDataInfo listOrderByUserId(SysMealOrder sysMealOrder)
    {
        startPage();
        Long userId = sysMealOrder.getUserId();
        if (StringUtils.isEmpty(String.valueOf(sysMealOrder.getUserId()))){
            userId = getUserId();
        }
        List<SysMealOrderVO> list = sysMealOrderService.listOrderByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 取消订单
     * @return
     */
    @PostMapping("/cancelOrder")
    public AjaxResult cancelOrder(String orderNumber){
        return sysMealOrderService.cancelOrder(orderNumber) > 0 ? success() : error();
    }
}
